﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;
using NPOI.OpenXmlFormats.Wordprocessing;

namespace ServiceWeb.DBBase
{
    public class DBBLanguageProductAlone
    {

        EFYihongIotContext ef = new EFYihongIotContext();
        public bs_language_product_alone getLanguagesConfig(string lp_guid)
        {
            return ef.bs_language_product_alone.Where(c => c.lp_guid == lp_guid).FirstOrDefault();
        }

        public List<bs_language_product_item_alone> getLanguagesConfigItems(string lpi_lp_guid)
        {
            return ef.bs_language_product_item_alone.Where(c => c.lpi_lp_guid == lpi_lp_guid).ToList();
        }
        public DataTable getLanguagesConfigItemsForDataTable(string lpi_lp_guid)
        {
            string sql = "select * from bs_language_product_item_alone inner join bs_language on lpi_l_guid=l_guid and lpi_lp_guid=@lpi_lp_guid";
            return MySqlYihongIot.GetDataTable(sql, new { lpi_lp_guid });
        }
        private static object obj = new object();
        public bool saveAll(bs_language_product_alone config)
        {
            lock (obj)
            {

                //先查一下有没重复提交
                bs_language_product_alone model = ef.bs_language_product_alone.Where(c => c.lp_guid == config.lp_guid).FirstOrDefault();
                if (model == null)
                {
                    //查编码
                    bs_language_product_alone getcode = ef.bs_language_product_alone.Where(c=>c.lp_type==0 && c.lp_guid == config.lp_guid).OrderByDescending(c => c.lp_id).FirstOrDefault();
                    if (getcode != null && getcode.lp_code != null)
                    {
                        config.lp_code = getcode.lp_code + 1;
                    }
                    else
                    {
                        config.lp_code = 200;
                    }
                    //写入
                    config.lp_add_time = DateTime.Now;
                    ef.bs_language_product_alone.Add(config);
                    ef.SaveChanges();

                }
                else
                {
                    model.lp_name = config.lp_name;
                    model.lp_add_time = DateTime.Now;
                    ef.bs_language_product_alone.Update(model);
                    ef.SaveChanges();
                }
            }
            return true;

        }
        public bool saveItem(bs_language_product_item_alone configItem)
        {

            //先查一下有没重复提交
            bs_language_product_item_alone model = ef.bs_language_product_item_alone.Where(c => c.lpi_guid == configItem.lpi_guid).FirstOrDefault();
            if (model == null)
            {
                //写入
                configItem.lpi_addtime = DateTime.Now;
                ef.bs_language_product_item_alone.Add(configItem);
                ef.SaveChanges();
            }


            return true;

        }

        public List<bs_language_product_item_alone> getPruductItemLanguage(string pguid, int languagecode)
        {
            List<bs_language_product_item_alone> config_Items =
              (from ci in ef.bs_language_product_item_alone
               join p in ef.bs_language_product_alone on ci.lpi_lp_guid equals p.lp_guid
               join l in ef.bs_language on ci.lpi_l_guid equals l.l_guid
               where p.lp_p_guid == pguid && l.l_code == languagecode
               select ci).ToList();
            return config_Items;

        }

        public List<bs_language_product_alone> getPruductLanguage(string pguid, int languagecode)
        {
            List<bs_language_product_alone> config_Items =
              (from ci in ef.bs_language_product_item_alone
               join p in ef.bs_language_product_alone on ci.lpi_lp_guid equals p.lp_guid
               join l in ef.bs_language on ci.lpi_l_guid equals l.l_guid
               where p.lp_p_guid == pguid && l.l_code == languagecode
               select p).ToList();
            return config_Items;

        }
    }

}
